//
//  WBFirstController.swift
//  WBTest
//
//  Created by pengZY on 2017/9/25.
//  Copyright © 2017年 zhang. All rights reserved.
//

import UIKit

class WBFirstController: WBBaseController {
    private var dataSources = ["1","2","3"]
    var isRefresh : Bool = false
    var ispull : Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()
 
     self.isTable = true
     self.title = "首页"
     self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, selector: #selector(toFriend),isBack:false)
    
      
        
    }
    
    override func refreshData() {
        
        if ispull {
            
            dataSources.append("111")
        }else{
            dataSources.insert("222", at: 0)
        }
        table.reloadData()
        refreshController.endRefreshing()
        isRefresh = false
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSources.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ad", for: indexPath)
        cell.textLabel?.text = dataSources[indexPath.row] as String
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = table.numberOfSections-1
      
       
        if section<0 || row<0 {
            return
        }
        let count = table.numberOfRows(inSection: section)
        
        if (count-1) == row && !isRefresh{
            isRefresh = true
            ispull = true
            refreshData()
            print("up")
        }else{
            isRefresh = false
            ispull = false
        }
    }
 }

extension WBFirstController{
    @objc func toFriend() -> () {
        self.navigationController?.pushViewController(WBMeController(), animated: true)
    }
}
